/**
 * 
 */
package com.rapwaves.mc.client.ui.site;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.rapwaves.mc.client.beans.SiteDetailsBean;
import com.rapwaves.mc.client.rpc.MembercareRemoteServiceAsync;
import com.rapwaves.mc.client.ui.HasInit;

/**
 * @author asanthan
 * 
 */
public class SiteDetailsTab extends Composite implements HasInit {

	private static SiteDetailsTabUiBinder uiBinder = GWT.create(SiteDetailsTabUiBinder.class);

	interface SiteDetailsTabUiBinder extends UiBinder<Widget, SiteDetailsTab> {
	}

	@UiField
	DeckPanel deckPanel;

	@UiField
	Label totalUsers;

	@UiField
	VerticalPanel newUsers;

	@UiField
	VerticalPanel activeUsers;

	public SiteDetailsTab() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		clear();
		deckPanel.showWidget(0);
		MembercareRemoteServiceAsync.INSTANCE
				.getSiteDetails(new OAuthAsnyCallBack<SiteDetailsBean>() {

					@Override
					public void onSuccess(SiteDetailsBean result) {
						deckPanel.showWidget(1);
						totalUsers.setText(result.getTotalUsers() + "");
						setUsers(newUsers, result.getNewUsers());
						setUsers(activeUsers, result.getActiveUsers());
					}

					private void setUsers(VerticalPanel userListPanel, List<String> userList) {
						if (userList == null || userList.isEmpty()) {
							userListPanel.add(new Label("No users"));
						} else {
							userListPanel.add(new Label(userList.size() + ""));
							for (String user : userList) {
								userListPanel.add(new Label(user));
							}
						}
					}
				});
	}

	/**
	 * 
	 */
	private void clear() {
		newUsers.clear();
		activeUsers.clear();
	}

}
